#!/bin/bash

qc=lova-qc
test=lova-test
pre=tls-pre
prod=tls-prod
qcenv=config-DQ
testenv=config-DQ
preenv=config-y3pre
prodenv=config-vspprod
lovabranchname=release-build
csbranchname=master
azure_ci_token=ivi6jkvviv5ll7hqlinjln4exyx5jynnfe2zodlzh7uosyb6vjea


if [[ $1 == "lova-pre" ]];then
if [[ $2 == "hotfix" ]];then
for service in `cat /root/script/svc.txt|grep -v ^#`
do
   qcimagename=`kubectl get deployments $service -n $qc -o json --kubeconfig=/root/.kube/$qcenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   qcver=`kubectl get deployments $service -n $qc -o json --kubeconfig=/root/.kube/$qcenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   preimagename=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   prever=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   if [[ ${qcimagename}:${qcver} == ${preimagename}:${prever} ]];then
      #echo "${qcimagename}:${qcver} equal ${preimagename}:${prever}"
      sleep 0.1
   else
      echo "${qcimagename}:${qcver} not equal ${preimagename}:${prever}"
      svcname=`echo $qcimagename|awk -F : '{print $NF}'`
      if [[ $`cat /root/script/lovasvc.txt|grep $svcname` ]];then
        curl -XPOST -u gitlab:$azure_ci_token https://vsrm.dev.azure.com/y3tech/LOVA/_apis/release/releases?api-version=7.0 -H 'Accept:application/json' -H 'Content-Type:application/json' -d "{\"definitionId\":15,\"variables\":{\"release_project\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$svcname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_version\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$qcver\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_branch\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$lovabranchname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}}}}"
      elif [[ $`cat /root/script/cssvc.txt|grep $svcname` ]];then
        curl -XPOST -u gitlab:$azure_ci_token https://vsrm.dev.azure.com/y3tech/LOVA/_apis/release/releases?api-version=7.0 -H 'Accept:application/json' -H 'Content-Type:application/json' -d "{\"definitionId\":33,\"variables\":{\"release_project\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$svcname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_version\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$qcver\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_branch\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$csbranchname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}}}}"
      fi
   fi
done
else
for service in `cat /root/script/svc.txt|grep -v ^#`
do
   testimagename=`kubectl get deployments $service -n $test -o json --kubeconfig=/root/.kube/$testenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   testver=`kubectl get deployments $service -n $test -o json --kubeconfig=/root/.kube/$testenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   preimagename=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   prever=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   if [[ ${testimagename}:${qcver} == ${preimagename}:${prever} ]];then
      #echo "${testimagename}:${testver} equal ${preimagename}:${prever}"
      sleep 0.1
   else
      echo "${testimagename}:${testver} not equal ${preimagename}:${prever}"
      svcname=`echo $testimagename|awk -F : '{print $NF}'`
      if [[ $`cat /root/script/lovasvc.txt|grep $svcname` ]];then
        curl -XPOST -u gitlab:$azure_ci_token https://vsrm.dev.azure.com/y3tech/LOVA/_apis/release/releases?api-version=7.0 -H 'Accept:application/json' -H 'Content-Type:application/json' -d "{\"definitionId\":15,\"variables\":{\"release_project\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$svcname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_version\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$testver\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_branch\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$lovabranchname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}}}}"
      elif [[ $`cat /root/script/cssvc.txt|grep $svcname` ]];then
        curl -XPOST -u gitlab:$azure_ci_token https://vsrm.dev.azure.com/y3tech/LOVA/_apis/release/releases?api-version=7.0 -H 'Accept:application/json' -H 'Content-Type:application/json' -d "{\"definitionId\":33,\"variables\":{\"release_project\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$svcname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_version\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$testver\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_branch\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$csbranchname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}}}}"
      fi
   fi
done
fi
elif [[ $1 == "lova-prod" ]];then
for service in `cat /root/script/svc.txt|grep -v ^#`
do
   preimagename=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   prever=`kubectl get deployments $service -n $pre -o json --kubeconfig=/root/.kube/$preenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   prodimagename=`kubectl get deployments $service -n $prod -o json --kubeconfig=/root/.kube/$prodenv|jq -r '.spec.template.spec.containers[].image'|awk -F/ '{print $NF}'|awk -F: '{print ($1)}'`
   prodver=`kubectl get deployments $service -n $prod -o json --kubeconfig=/root/.kube/$prodenv|jq -r '.spec.template.spec.containers[].image'|awk -F: '{print $NF}'`
   if [[ ${preimagename}:${prever} == ${prodimagename}:${prodver} ]];then
      #echo "${preimagename}:${prever} equal ${prodimagename}:${prodver}"
      sleep 0.1
   else
      echo "${preimagename}:${prever} not equal ${prodimagename}:${prodver}"
      svcname=`echo $preimagename|awk -F : '{print $NF}'`
      if [[ $`cat /root/script/lovasvc.txt|grep $svcname` ]];then
        curl -XPOST -u gitlab:$azure_ci_token https://vsrm.dev.azure.com/y3tech/LOVA/_apis/release/releases?api-version=7.0 -H 'Accept:application/json' -H 'Content-Type:application/json' -d "{\"definitionId\":16,\"variables\":{\"release_project\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$svcname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_version\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$prever\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_branch\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$lovabranchname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}}}}"
      elif [[ $`cat /root/script/cssvc.txt|grep $svcname` ]];then
        curl -XPOST -u gitlab:$azure_ci_token https://vsrm.dev.azure.com/y3tech/LOVA/_apis/release/releases?api-version=7.0 -H 'Accept:application/json' -H 'Content-Type:application/json' -d "{\"definitionId\":34,\"variables\":{\"release_project\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$svcname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_version\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$prever\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}},\"release_branch\":{\"allowOverride\":true,\"isSecret\":false,\"value\":\"$csbranchname\",\"scope\":{\"key\":2147483647,\"value\":\"Release\",\"isDefault\":true}}}}"
      fi
   fi
done
fi
